﻿using System.Collections.Generic;
using System.Security.Cryptography;
using B.S.DataBase.MyProject.Domain.SaleOrders;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using Consul;
using MediatR;
using Microsoft.AspNetCore.Http;
using T.E.ERP.Read.API.Applicantions.Command.MyOrdermementCommands;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.MyOrdermementHandlers
{
    public class MyOrdermementHandler : IRequestHandler<SalemementCommand, APIResult<List<MyMementDTO>>>
    {
        private readonly IBaseRepository<InfrastrucModel> _infrastrucModel;
        private readonly IBaseRepository<MaterialSupplier> _materialSupplier;
        private readonly ILogger<MyOrdermementHandler> _logger;
        public MyOrdermementHandler(IBaseRepository<InfrastrucModel> infrastrucModel, ILogger<MyOrdermementHandler> logger, IBaseRepository<MaterialSupplier> materialSupplier)
        {
            _infrastrucModel = infrastrucModel;
            _logger = logger;
            _materialSupplier = materialSupplier;
        }

        public Task<APIResult<List<MyMementDTO>>> Handle(SalemementCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<List<MyMementDTO>>
            {
                Code = APIEnum.查询成功,
                Msg = APIEnum.查询成功.ToString()
            };
            try
            {
                var list = from a in _infrastrucModel.GetAll().Result
                           join b in _materialSupplier.GetAll().Result on a.GongName equals b.Supid
                           select new MyMementDTO
                           {
                               Oid=a.Oid,
                               GetMaintitle = a.GetMaintitle,
                               OrderCode = a.OrderCode,
                               JoinGetOrderid = a.JoinGetOrderid,
                               JoinShouOrderid = a.JoinShouOrderid,
                               SaleDate = a.SaleDate,
                               GongName = a.GongName,
                               SupplierName = b.SupplierName,
                               LianName = a.LianName,
                               PhoneNumber = a.PhoneNumber,
                               ByName = a.ByName,
                               ByDepart = a.ByDepart,
                               LianPhone = a.LianPhone,
                               JoinProject = a.JoinProject,
                               SuanType = a.SuanType,
                               Address = a.Address,
                               Remember = a.Remember,
                               SumMoney = a.SumMoney,
                               YfuMoney = a.YfuMoney,
                               YdpMoney = a.YdpMoney,
                               ShenpStuct = a.ShenpStuct,
                               FukuanStuct = a.FukuanStuct,
                               RukuStuct = a.RukuStuct,
                               DpiaoStuct = a.DpiaoStuct
                           };
                if (!string.IsNullOrEmpty(request.OrderCode))
                {
                    list = list.Where(x => x.OrderCode == request.OrderCode);
                }

                _logger.LogInformation("查询订单工单列表成功");
                result.data = list.ToList();
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = "查询过程中发生错误";
                _logger.LogError(ex, "查询基础设施模型时发生错误");
                throw;
            }
            return Task.FromResult(result);
        }
    }
}
